<?php
    /*  @name       Regrid PHP CSS builder
     *  @author     Alex Carver <alexander.carver.bsd@gmail.com>
     *  @copyright  Alex Carver 2011
     *  @link       https://code.google.com/p/regrid/
     *  
     *  Copyright (c) 2011, Alexander Carver [alexander.carver.bsd@gmail.com]
     *  All rights reserved.
     *
     *  Redistribution and use in source and binary forms, with or without
     *  modification, are permitted provided that the following conditions are met:
     *      * Redistributions of source code must retain the above copyright
     *        notice, this list of conditions and the following disclaimer.
     *      * Redistributions in binary form must reproduce the above copyright
     *        notice, this list of conditions and the following disclaimer in the
     *        documentation and/or other materials provided with the distribution.
     *      * Neither the name of the contributors' employers nor the
     *        names of its contributors may be used to endorse or promote products
     *        derived from this software without specific prior written permission.
     *
     *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
     *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY
     *  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     */

    header('Content-type: text/css',true);
    ob_start();
?>
.g, .g .g-c{display:block;margin:0;padding:0;border:0;zoom:1;}
.g{margin:0 auto 0 auto;}
.g .g-c{float:left;}
.g:after, .g-c:after{content:"_";display:block;height:0;clear:both;visibility:hidden;}
<?php

    $cols = 16;
    $sizes = isset($_REQUEST['w']) ? explode(',', $_REQUEST['w']) : array();
    
    $n_sizes = count($sizes);
    
    for($i=1; $i<($cols+1); $i++)
        printf(".g .g-%s{width:%s%%}", $i, ($i/$cols) * 100);
    
    for($i=$n_sizes-1; $i>-1; $i--)
        print ".g-{$sizes[$i]}{width:{$sizes[$i]}px}";
    
    for($i=$n_sizes-1; $i>-1; $i--){
        print "@media all and (max-width:{$sizes[$i]}px){";
        print ".g .g-0-lt-{$sizes[$i]}{display:none}";
        for($j=1; $j<($cols+1); $j++)
            printf(".g .g-%s-lt-%s{width:%s%%}", $j, $sizes[$i], ($j/$cols) * 100);
        print "}";
    }
    
    foreach($sizes as $i=>$size){
        print "@media all and (min-width:".($i ? $size : 0)."px){";
        print ".g-all,";
        print ".g-{$size}{width:{$size}px}";
        print "}";
    }
    
    print preg_replace('/\s+/', ' ', str_replace("\n", '', ob_get_clean()));
?>